import 'package:flutter/material.dart';
import 'package:flutter_class2/3681/accountRelated/myPageView.dart';

import 'NewsPage.dart';
import 'PersonPage.dart';

class MainPage extends StatelessWidget {
  String idStr;
  MainPage({Key key, this.idStr, }) : super( key: key);

  Widget build(BuildContext context){
    return MainPageState(key: key, idStr: idStr,);
  }
}

class MainPageState extends StatefulWidget {
  String idStr;
  MainPageState({Key key, this.idStr, }) : super( key: key);

  @override
  _MainPageState createState() => new _MainPageState();
}

class _MainPageState extends State<MainPageState>{

  int currentIndex = 0;// 当前页面

  PageController pageController = new PageController(initialPage: 0);
  @override
  Widget build(BuildContext context) {

    final List<BottomNavigationBarItem> bottomNavItems = [
      BottomNavigationBarItem(
        // backgroundColor: Colors.blue,
        icon: Icon(Icons.home, color: Colors.black54,),
        title: Text("首页", style: TextStyle(color: Colors.black54),),
      ),
      BottomNavigationBarItem(
        // backgroundColor: Colors.cyan,
        icon: Icon(Icons.account_circle_rounded, color: Colors.black54,),
        title: Text("我的", style: TextStyle(color: Colors.black54),),
      ),
    ];

    final pages = [NewsPage(), PersonPage()];

    final headPic = Hero(
      tag: 'headPic',
      child: Padding(
        padding: EdgeInsets.all(8.0),
        child: CircleAvatar(
          radius: 72.0,
          backgroundColor: Colors.transparent,
          backgroundImage: AssetImage('images/meng.png',),
        ),
      ),
    );

    final appBarTitle = Text('新闻纵览');

    return Scaffold(
      resizeToAvoidBottomInset: false,// 防止弹出输入法时内容溢出屏幕
      appBar: AppBar(
        leading: Builder(
          builder: (BuildContext context) {
            return InkWell(
              child: headPic,
              onTap: (){
                Navigator.pushNamed(context, '/mengIntroduction');
              },
            );
          },
        ),
        title: appBarTitle,
      ),
      body: CustomPageView.builder( // 使用自定义的PageView（修改cacheExtent）以实现页面保活
        onPageChanged: _onPageChanged,
        controller: pageController,
        itemCount: pages.length,
        itemBuilder: (BuildContext context, int index) {
          return pages[index];
        },
      ),
      bottomNavigationBar: Hero(
          tag: 'loginButton',
          child:BottomNavigationBar(
            items: bottomNavItems,
            currentIndex: currentIndex,
            type: BottomNavigationBarType.fixed,
            onTap: (index){
              _onTapHandler(index);
            },
          )
      ),
    );
  }

  //底部导航栏按钮点击事件
  //动画切换页面
  void _onTapHandler(int index) {
    pageController.animateToPage(index,
        duration: Duration(milliseconds: 100), curve: Curves.ease);
  }

  //滑动切换页面
  void _onPageChanged(int index) {
    /*如果点击的导航项不是当前项  切换 */
    if (index != currentIndex) {
      setState(() {
        currentIndex = index;
      });
    }
  }
}